<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="search">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话号码" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!-- <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 340px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row class="mb8 action-box">
      <el-upload action="/上传文件的接口" :on-change="updateVipClick" :auto-upload="false" :show-file-list="false" accept=".xls, .xlsx">
        <el-button type="warning" size="mini" v-hasPermi="['member:consumer:batchGive']" icon="el-icon-folder-add">批量赠送</el-button>
      </el-upload>

      <!-- <el-button class="oneBtn" type="warning" size="mini" icon="el-icon-folder-add" v-hasPermi="['member:consumer:oneGive']" @click="oneSend">单个赠送</el-button> -->
    </el-row>
    <el-table :height="tableHeight" v-loading="loading" @sort-change="changeTableSort" :data="list" @row-dblclick="handleUpdate" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="创建时间" prop="createTime" width="160" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="UID" prop="userId" align="center" :sortable="'userId'" />
      <el-table-column label="昵称" prop="userNickName" align="center" />
      <el-table-column label="用户电话" prop="phone" align="center" width="150" />
      <el-table-column label="抽奖次数" prop="num" align="center" width="150" />
      <el-table-column label="操作人" prop="sysUserName" align="center" />
      <el-table-column label="说明" prop="remark" min-width="200" />
      <!-- <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="" @click="handleUpdate(scope.row)" v-hasPermi="['']">查看</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 批量赠送 -->
    <z-dialog :title="title" :visible.sync="showExcelView" :isShowConfirm="false">
      <upload-tabel :list="excelList" @submit="submit"></upload-tabel>
    </z-dialog>

    <!-- 单个会员赠送 -->
    <z-dialog :title="title" :visible.sync="showSingleDialog" @confirm="singleSubmit('single')" @cancel="cancel('single')">
      <single-gift ref="single"></single-gift>
    </z-dialog>

  </div>
</template>

<script>
import { listSendPrize, excelSendPrize } from "@/api/vip/vip.js";
import { uploadExcel } from './uploadExcel'
import uploadTabel from './uploadTabel'
import { vipLevelEnum } from '@/common/enums'
import singleGift from './singleGift'
export default {
  name: "sendPrize", //会员赠送
  components: { uploadTabel, singleGift },
  data() {
    return {
      vipLevelEnum,
      showExcelView: false,
      showSingleDialog: false,
      // 列表数据
      list: [],
      excelList: [],//导入的excelList
      dateRange: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      updateCashShow: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      pageNum: 1,
      // 表单参数
      form: {},
      rules: {
      },
    };
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    dateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createTimeStart = String(newV[0]);
          this.queryParams.createTimeEnd = String(newV[1]);
        }
      } else {
        this.queryParams.createTimeStart = undefined;
        this.queryParams.createTimeEnd = undefined;
      }
      this.getList();

    }
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listSendPrize(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.list = response.data;
          this.total = response.total;
        }
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      });
    },

    changeTableSort(column) {
      this.queryParams.orderBy = column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正
      this.getList();
    },

    // 表单重置
    reset() {
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.createTimeStart = undefined;
      this.queryParams.createTimeEnd = undefined;
      this.dateRange = undefined;

      this.resetForm("queryForm");
      this.handleQuery();
    },

    updateVipClick(file, fileList) {
      let _this = this;
      this.title = "抽奖次数赠送";

      uploadExcel(file, (res) => {
        _this.excelList = res;
        _this.showExcelView = true;
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "消费券赠送";
      this.form = row;
    },

    cancel(refStr) {
      let ref = this.$refs[refStr];
      ref.reset();
    },

    //单个赠送
    oneSend() {
      this.title = '单个会员赠送';
      this.showSingleDialog = true;
    },

    //单个赠送提交
    singleSubmit(refStr) {
      let ref = this.$refs[refStr];
      let params = ref.params;
      params.userId = ref.queryParams.search.id;
      ref.$refs["form"].validate((valid) => {
        if (valid) {
          sendConsume(params).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.showSingleDialog = false;
            this.getList();
          });
        }
      });
    },

    /** 批量赠送 */
    submit(data) {
      data.remark = String(data.remark);
      excelSendPrize(data).then((response) => {
        if (response.code == 500) {
          this.$modal.msgError(response.data.msg);
        } else {
          this.$modal.msgSuccess("操作成功");
          this.showExcelView = false;
          this.getList();
        }
      }).catch(err => {
        console.log(err);
      });
    }
  },
};
</script>
<style lang="scss" scoped>
.action-box {
  display: flex;
  .oneBtn {
    margin-left: 12px;
  }
}
</style>

